package com.yueze.demo1.dao;

import com.yueze.demo1.entities.Image1;
import org.springframework.stereotype.Repository;

import java.sql.*;

/**
 * @author Administrator
 * @create 2020-08-04
 * 16:14
 * @desc
 **/
public class SqlserUtil {
    private static Connection conn = SqlserUtil.getConnection();;
    private Statement stmt = null;
    private ResultSet rs = null;
    //private static Image1 image = new Image1();
    public static Connection getConnection() {
        String driveName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        //String url = "jdbc:sqlserver://192.168.2.161\\GBLHTWIN7\\TEST;DatabaseName=hzsrtestdb";
        String url = "jdbc:sqlserver://192.168.2.161:1433;DatabaseName=hzsrtestdb";
        String userName = "sa";
        String passWord = "123456";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(url, userName, passWord);
            System.out.println("===============ok================");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void insert(Image1 image){
        //数据库查询语言，这里是study的全表查询

        String img_name = image.getImg_name();
        String img_url = image.getImg_url();
        String img_size = image.getImg_size();

        //插入语句，在study里面插入值为（1205, 'C999', 33）的三个值
        String sqlinsert = "insert into imgadrr(img_url,img_name,img_size,img_height,img_width)" +
        " VALUES('"+img_url+"','"+img_name+"','"+img_size+"',1,1)";

        /*String sqlinsert = "insert into imgadrr(img_url,img_name,img_postfix,img_height,img_width)" +
                " VALUES('sdds','sdf','dsf',1,1)";*/
        //String sqldelete = "delete from study where grade='33'";

        // 建立Statement对象
        try {
            stmt = conn.createStatement();

            //插入数据到数据库里面
            stmt.executeUpdate(sqlinsert);
            //stmt.execute(sqldelete);//SQL删除语句

        } catch (SQLException e) {
            this.cloes1();
        e.printStackTrace();
    }

    }

    public void select1(){
        String sql = "select * from imgadrr";
        // 执行数据库查询语句
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);//执行

            while (rs.next()) {
                String img_url = rs.getString("img_url");
                System.out.println("addr" + img_url + "\tcno:" + "\tname:");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void cloes1(){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("关闭失败");
            }
        }
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public void cloes2(){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("关闭失败");
            }
        }
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}
